#include "PeopleCircle.h"
#include "Person.h"
#include "PlayerObject.h"
#include "PlayerObjectManager.h"
#include "PlayerProxy.h"
#include "PathPlanner.h"
#include "pause.h"
#include "stdlib.h"

PeopleCircle::PeopleCircle( const unsigned int unMaxPeopleNum ) :
m_unMaxPeopleNum( unMaxPeopleNum )
{
    Init();
}

PeopleCircle::~PeopleCircle()
{
    UnInit();
}

void PeopleCircle::Social()
{
    while ( true )
    {
        for ( NameToPersonMap::iterator itLoop = m_mapNameToPersons.begin(); m_mapNameToPersons.end() != itLoop; ++itLoop )
        {
            Person* pTheone = itLoop->second;

            if ( NULL == pTheone )
            {
                continue;
            }

            if ( pTheone->IsActionDone() )
            {
                pTheone->Action( rand() % PurposeMax );
            }
            else
            {
                pTheone->StepMove();
            }
        }

        util::pause( 1 );
    }
}

void PeopleCircle::Init()
{
    const std::vector<PlayerObject>& refPersons = PlayerObjectManager::Instance().GetDynamicObjects();

    int nId = 1000;
    unsigned int nCount = 0u;
    for ( std::vector<PlayerObject>::const_iterator itLoop = refPersons.begin(); refPersons.end() != itLoop; ++itLoop )
    {
        Position stPos = { itLoop->PosLowX + itLoop->SizeX / 2.0, itLoop->PosLowY + itLoop->SizeY / 2.0, itLoop->Yaw };
        Person* pNewOne = new Person( nId, stPos );
        m_mapNameToPersons.insert( NameToPersonMap::value_type( itLoop->Name.c_str(), pNewOne ));
        m_mapDevIdToPersons.insert( DevIdToPersonMap::value_type( nId, pNewOne ));
        ++nId;
        ++nCount;

        if ( nCount >= m_unMaxPeopleNum )
        {
            break;
        }
    }

    PlayerProxy::Instance().PopulatePosProxy( m_mapNameToPersons.size() );
}

void PeopleCircle::UnInit()
{
    for ( NameToPersonMap::iterator itLoop = m_mapNameToPersons.begin(); m_mapNameToPersons.end() != itLoop; ++itLoop )
    {
        delete itLoop->second;
        itLoop->second = NULL;
    }

    m_mapNameToPersons.clear();
    m_mapDevIdToPersons.clear();
}

void PeopleCircle::RefreshPositions()
{
    std::vector<PlayerObject>& refPersons = PlayerObjectManager::Instance().GetDynamicObjects();

    PlayerProxy::Instance().RefreshProxy();

    for ( std::vector<PlayerObject>::iterator itLoop = refPersons.begin(); refPersons.end() != itLoop; ++itLoop )
    {
        NameToPersonMap::iterator itFound = m_mapNameToPersons.find( itLoop->Name );
        if ( itFound == m_mapNameToPersons.end() )
        {
            continue;
        }

        if ( NULL == itFound->second )
        {
            continue;
        }

        Position& posNow = itFound->second->GetLatestPos();

        itLoop->updatePos( posNow.x, posNow.y, posNow.yaw );

        Velocity& velNow = itFound->second->GetLatestVelocity();

        itLoop->updateVel( velNow.x, velNow.y, velNow.yaw );
    }
}

